package com.IntelligentCommunity.community.service.impl;

import com.IntelligentCommunity.community.domain.RealArea;
import com.IntelligentCommunity.community.domain.RealDistrict;
import com.IntelligentCommunity.community.domain.Tree;
import com.IntelligentCommunity.community.mapper.RealAreaMapper;
import com.IntelligentCommunity.community.service.IRealAreaService;
import com.IntelligentCommunity.community.service.IRealDistrictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 美国五星上将麦克阿瑟
 */
@Service
public class RealAreaServiceImpl extends ServiceImpl<RealAreaMapper, RealArea> implements IRealAreaService {

    @Autowired
    private IRealDistrictService districtService;

    @Autowired
    private RealAreaMapper areaMapper;

    private int flag;

    /**
     * 根据区域信息查询区域树。
     *
     * @return 包含所有区域和下属行政区划的树结构列表。
     */
    @Override
    public List<Tree> selectAreaTree(int flag) {

        // 标记，0->查询至小区 1->查询至辖区
        this.flag = flag;

        // 查询所有区域
        List<Tree> allArea = areaMapper.listTree();
        // 查询所有小区
        List<Tree> allDistrict = districtService.selectTree();

        // 过滤出根节点区域，并构建区域树
        return allArea.stream()
            .filter(item -> 0 == item.getParentId())
            .map(item -> {
                Tree realArea = new Tree();
                BeanUtils.copyProperties(item, realArea);
                // 为区域节点添加子节点
                realArea.setChildren(getChildrenList(allArea, item, allDistrict));
                return realArea;
            }).collect(Collectors.toList());
    }

    /**
     * 获取指定区域的子节点列表。
     *
     * @param allArea 所有区域列表
     * @param area 当前区域
     * @param allDistrict 所有行政区划列表
     * @return 指定区域的子节点列表
     */
    private List<Tree> getChildrenList(List<Tree> allArea, Tree area, List<Tree> allDistrict) {

        List<Tree> returnList = new ArrayList<>();

        Long parentId = area.getId();

        // 过滤出当前区域的子区域，并构建区域树
        List<Tree> treeList = allArea.stream().filter(item -> parentId.equals(item.getParentId())).collect(Collectors.toList());

        returnList = treeList.stream()
            .map(item -> {
                Tree areaTree = new Tree();
                BeanUtils.copyProperties(item, areaTree);
                areaTree.setChildren(getChildrenList(allArea, item, allDistrict));
                // 如果区域没有子区域，则添加下属行政区划
                if (areaTree.getChildren() == null && flag == 0) {
                    List<Tree> districtTreeList = allDistrict.stream()
                        .filter(district -> item.getId().equals(district.getParentId()))
                        .map(dis -> {
                            Tree districtTree = new Tree();
                            BeanUtils.copyProperties(dis, districtTree);
                            return districtTree;
                        }).collect(Collectors.toList());
                    areaTree.setChildren(districtTreeList);
                }
                return areaTree;
            }).collect(Collectors.toList());

        return treeList.isEmpty() ? null : returnList;
    }
}
